package middleware

import (
	"gin/chibuda/app/utils"
	"gin/chibuda/system/storage"
	"github.com/gin-gonic/gin"
)

//后台菜单权限
func AdminAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		role_id,_:=c.Get("role_id")
		if role_id.(float64) != 1 {
			path:=utils.GetBetweenStrs(c.Request.RequestURI,"/","?")
			res,_:=storage.DB().Table("amdin_role_menu a").
				Join("admin_menu b","a.menu_id","=","b.id").
				Where("a.role_id",role_id).
				Where("b.path",path).
				Where("b.method",c.Request.Method).
				First()
			if res==nil {
				c.JSON(utils.TokenErrCode, gin.H{"msg":"没有访问权限"})
				c.Abort()
				return
			}
		}
	}
}